package com.lancoo.ccas52.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.ccas52.arrangecourse.ArrangeCourseService;
import com.lancoo.ccas52.aspect.LogAnnotation;
import com.lancoo.ccas52.entity.CollegeStatus;
import com.lancoo.ccas52.entity.Schedule;
import com.lancoo.ccas52.entity.ScheduleConflictInfo;
import com.lancoo.ccas52.entity.TaskInfo;
import com.lancoo.ccas52.pojo.common.PageInfo;
import com.lancoo.ccas52.pojo.dto.ArrangeTeachingClassDto;
import com.lancoo.ccas52.pojo.dto.ScheduleDto;
import com.lancoo.ccas52.pojo.vo.*;
import com.lancoo.ccas52.service.CollegeStatusService;
import com.lancoo.ccas52.service.ScheduleService;
import com.lancoo.ccas52.service.TaskInfoService;
import com.lancoo.ccas52.util.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "编排课表")
@RestController
@RequestMapping("/api/v5.2.1/pc/arrange")
public class CourseArrangeController {

    @Resource
    private ScheduleService scheduleService;
    @Resource
    private ArrangeCourseService arrangeCourseService;
    @Resource
    private TaskInfoService taskInfoService;
    @Resource
    private CollegeStatusService collegeStatusService;

    @ApiOperation("获取课程教学班列表")
    @GetMapping(value = "/list/course")
    public ResponseObject<PageInfo<TeachingClassCourseDto>> getTeachingClassCourse(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                                   @ApiParam(value = "排课状态 1已排（正常）、2已排（冲突）、3待排", required = false) @RequestParam(required = false) Integer state,
                                                                                   @ApiParam(value = "1未锁定  2锁定", required = false) @RequestParam(required = false) Integer isLocked,
                                                                                   @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                                                   @ApiParam(value = "修读性质 1主 2辅修", required = false) @RequestParam(required = false) Integer studyNature,
                                                                                   @ApiParam(value = "专业性质，1:公共课，2:专业课", required = false) @RequestParam(required = false) Integer majorProp,
                                                                                   @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                                   @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<TeachingClassCourseDto> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);
        return ResponseObject.ofSuccess(scheduleService.getTeachingClassCoursePage(taskId, page, state, isLocked, collegeId, studyNature, majorProp));
    }

    @ApiOperation("获取环节教学班列表")
    @GetMapping(value = "/list/segment")
    public ResponseObject<PageInfo<TeachingClassSegmentDto>> getTeachingClassSegment(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                                     @ApiParam(value = "1正常  2冲突", required = false) @RequestParam(required = false) Integer state,
                                                                                     @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                                                     @ApiParam(value = "修读性质 1主 2辅修", required = false) @RequestParam(required = false) Integer studyNature,
                                                                                     @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                                     @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<TeachingClassSegmentDto> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);
        return ResponseObject.ofSuccess(scheduleService.getTeachingClassSegmentPage(taskId, page, state, collegeId, studyNature));
    }

    @ApiOperation("根据教学班id获取周次课表信息")
    @GetMapping(value = "getWeekSchedule")
    public ResponseObject<TeachingClassCourseDto> getWeekSchedule(@ApiParam(value = "教学班id", required = true) @RequestParam(required = true) Long teachingClassId) {
        return ResponseObject.ofSuccess(scheduleService.getWeekSchedule(teachingClassId));
    }

    @ApiOperation("获取该教学班冲突")
    @GetMapping(value = "getScheduleConflictInfo")
    public ResponseObject<List<ScheduleConflictInfo>> getScheduleConflictInfo(@ApiParam(value = "教学班id", required = true) @RequestParam(required = true) Long teachingClassId) {
        return ResponseObject.ofSuccess(scheduleService.getScheduleConflictInfo(teachingClassId));
    }

    @ApiOperation("获取学院锁定的课程教学班数量")
    @GetMapping(value = "/locks")
    public ResponseObject<Long> getAllLockedTeachingClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                          @ApiParam(value = "院系id", required = false) @RequestParam(required = false) String collegeId,
                                                          @ApiParam(value = "专业性质，1:公共课，2:专业课", required = false) @RequestParam(required = false) Integer majorProp) {
        return ResponseObject.ofSuccess(scheduleService.getAllLockedTeachingClass(taskId, collegeId, majorProp));
    }

    @ApiOperation("修改锁定状态")
    @PutMapping(value = "/updateLock")
    public ResponseObject<Boolean> updateLock(@ApiParam(value = "1解锁  2锁定", required = true) @RequestParam Integer isLocked,
                                              @ApiParam(value = "教学班id", required = true) @RequestBody List<Long> teachingClassIds) {
        return ResponseObject.ofSuccess(scheduleService.updateLock(isLocked, teachingClassIds));
    }

    @ApiOperation("锁定院系所有正常教学班")
    @PutMapping(value = "/lockedNormal")
    public ResponseObject<Boolean> lockedNormal(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                @ApiParam(value = "院系id", required = false) @RequestParam(required = false) String collegeId,
                                                @ApiParam(value = "专业性质，1:公共课，2:专业课", required = false) @RequestParam(required = false) Integer majorProp) {
        return ResponseObject.ofSuccess(scheduleService.allNormalLocal(taskId, collegeId, majorProp));
    }

    @ApiOperation("解锁院系所有教学班")
    @PutMapping(value = "/allUnLock")
    public ResponseObject<Boolean> allUnLock(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                             @ApiParam(value = "院系id", required = false) @RequestParam(required = false) String collegeId,
                                             @ApiParam(value = "专业性质，1:公共课，2:专业课", required = false) @RequestParam(required = false) Integer majorProp) {
        return ResponseObject.ofSuccess(scheduleService.allUnLock(taskId, collegeId, majorProp));
    }

    @ApiOperation("获取课表编排统计信息")
    @GetMapping(value = "/arrangeStatistics")
    public ResponseObject<PublishStatisticsArrange> getPublishStatisticsArrange(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                                @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                                                @ApiParam(value = "专业性质，1:公共课，2:专业课", required = false) @RequestParam(required = false) Integer majorProp) {
        return ResponseObject.ofSuccess(scheduleService.getPublishStatisticsArrange(taskId, collegeId, majorProp));
    }

    @ApiOperation("自动排课")
    @GetMapping(value = "/autoArrangement")
    public ResponseObject<List<ScheduleConflictInfo>> courseArrangement(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                        @ApiParam(value = "院系id", required = false) @RequestParam(required = false) String collegeId,
                                                                        @ApiParam(value = "专业性质，1:公共课，2:专业课", required = false) @RequestParam(required = false) Integer majorProp) {
//        //fix bug 修复自动排课时，前端全校排课时，传入的collegeId不为null
//        collegeId = majorProp == null ? null : collegeId;
        return ResponseObject.ofSuccess(scheduleService.autoArrangement(taskId, collegeId, majorProp));
    }

    @ApiOperation("获取一个教学班信息")
    @GetMapping(value = "/getById")
    public ResponseObject<TeachingClassCourseDto> getTeachingClassById(@ApiParam(value = "教学班id", required = true) @RequestParam Long teachingClassId) {
        return ResponseObject.ofSuccess(scheduleService.getTeachingClassById(teachingClassId));
    }

    @ApiOperation("生成教学班课表初始数据")
    @PostMapping(value = "/generateTeachingClassUnit")
    public ResponseObject<List<Schedule>> generateTeachingClassUnit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                    @ApiParam(value = "教学班id", required = true) @RequestParam Long teachingClassId) {
        return ResponseObject.ofSuccess(scheduleService.generateTeachingClassUnit(taskId, teachingClassId));
    }

    @ApiOperation("根据教学班id删除课表")
    @DeleteMapping(value = "/removeSchedule")
    public ResponseObject<Boolean> generateTeachingClassUnit(@ApiParam(value = "教学班id", required = true) @RequestParam Long teachingClassId) {
        return ResponseObject.ofSuccess(scheduleService.removeSchedule(teachingClassId));
    }

    @ApiOperation("获取调整场地节次 列表")
    @PostMapping(value = "/getVenueSession")
    public ResponseObject<PageInfo<VenueSessionVo>> getVenueSession(@ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                    @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize,
                                                                    @ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                    @ApiParam(value = "调整周次 1表示待调整周次", example = "101010101", required = true) @RequestParam String weeks,
                                                                    @ApiParam(value = "节次课时编码，多节次逗号分割", example = "0102,0103", required = false) @RequestParam(required = false) String timeCodes,
                                                                    @ApiParam(value = "教室id", required = false) @RequestParam(required = false) Long roomId,
                                                                    @ApiParam(value = "院系id,公共课传空字符串，确定排课传null", required = false) @RequestParam(required = false) String collegeId,
                                                                    @ApiParam(value = "教学班相关信息", required = true) @RequestBody VenueSessionInput venueSessionInput) {
        TaskInfo taskInfo = taskInfoService.getById(taskId);
        //全校统一排课 校区id置为null获取全部
        if (taskInfo.getUnifiedCampus().equals(1)) {
            venueSessionInput.setCampusId(null);
        }
        ArrangeTeachingClassDto teachingClass = BeanUtil.copyProperties(venueSessionInput, ArrangeTeachingClassDto.class);
        teachingClass.setProhibitTimeslotCodes("");
        return ResponseObject.ofSuccess(scheduleService.getVenueSession(currentPage, pageSize, taskId, weeks, timeCodes, roomId, collegeId, teachingClass));
    }

    @ApiOperation("查看空闲课表")
    @PostMapping(value = "/getFreeVenueSession")
    public ResponseObject<PageInfo<VenueSessionVo>> getFreeVenueSession(@ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                        @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize,
                                                                        @ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                        @ApiParam(value = "调整周次 1表示待调整周次", example = "101010101", required = true) @RequestParam String weeks,
                                                                        @ApiParam(value = "节次编码，多节次逗号分割", example = "01,02", required = false) @RequestParam(required = false) String sections,
                                                                        @ApiParam(value = "连上节次", required = true) @RequestParam(required = true) Integer connectSection,
                                                                        @ApiParam(value = "楼宇id", required = false) @RequestParam(required = false) Integer buildingId,
                                                                        @ApiParam(value = "场地类型", required = false) @RequestParam(required = false) Integer roomType,
                                                                        @ApiParam(value = "周次编码，多周次逗号分割", example = "01,02", required = false) @RequestParam(required = false) String week,
                                                                        @ApiParam(value = "教室id", required = false) @RequestParam(required = false) Long roomId,
                                                                        @ApiParam(value = "院系id,公共课传空字符串，确定排课传null", required = false) @RequestParam(required = false) String collegeId) {
        return ResponseObject.ofSuccess(scheduleService.getFreeVenueSession(currentPage, pageSize, taskId, weeks, sections, connectSection, buildingId, roomType, week, roomId, collegeId));
    }


    @ApiOperation("更新场地节次")
    @PostMapping(value = "/updateVenueSession")
    public ResponseObject<List<ScheduleDto>> updateVenueSession(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                @ApiParam(value = "学校Id", required = true) @RequestParam String schoolId,
                                                                @ApiParam(value = "学期", required = true) @RequestParam String term,
                                                                @ApiParam(value = "调整至课表", required = true) @RequestBody UpdateVenueSessionInput updateVenueSessionInput) {
        List<ScheduleDto> scheduleDtos = scheduleService.updateVenueSession(updateVenueSessionInput.getStartSchedule(), updateVenueSessionInput.getStopSchedule(), updateVenueSessionInput.getTeachingClassId());
        //更新冲突表 排课状态
        arrangeCourseService.manualAdjustmentUpdate(schoolId, term, taskId, null, updateVenueSessionInput.getTeachingClassId());
        return ResponseObject.ofSuccess(scheduleDtos);
    }

    @ApiOperation("修改院系相关设置")
    @PostMapping(value = "/arrangeSubmit")
    public ResponseObject<CollegeStatus> arrangeSubmit(@ApiParam(value = "TeachingClassInput对象", required = true) @RequestBody CollegeStatus collegeStatus) {
        return ResponseObject.ofSuccess(scheduleService.arrangeSubmit(collegeStatus));
    }

    @ApiOperation("修改院系相关设置")
    @PostMapping(value = "/removeArrangeSubmit")
    public ResponseObject<Object> removeArrangeSubmit(@ApiParam(value = "TeachingClassInput对象", required = true) @RequestBody CollegeStatus collegeStatus) {
        scheduleService.removeArrangeSubmit(collegeStatus);
        return ResponseObject.ofSuccess();
    }

    @ApiOperation("获取院系相关设置")
    @GetMapping(value = "/getArrangeSubmit")
    public ResponseObject<CollegeStatus> arrangeSubmit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                       @ApiParam(value = "学院Id", required = false) @RequestParam(required = false) String collegeId) {
        CollegeStatus collegeStatus = collegeStatusService.lambdaQuery().eq(CollegeStatus::getTaskId, taskId).eq(CollegeStatus::getCollegeId, collegeId).one();
        if (ObjUtil.isNull(collegeStatus)) {
            collegeStatus = new CollegeStatus()
                    .setTaskId(taskId).setCollegeId(collegeId).setArrangeSubmit(0).setIsLock(0);
        }
        return ResponseObject.ofSuccess(collegeStatus);
    }

    @ApiOperation("确定排课结果列表")
    @GetMapping(value = "/arrangeDetermineStatistics")
    public ResponseObject<List<DetermineStatistics>> arrangeDetermineStatistics(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                                @ApiParam(value = "学院Id", required = false) @RequestParam(required = false) String collegeId,
                                                                                @ApiParam(value = "学院名模糊查询", required = false) @RequestParam(required = false) String keyWord,
                                                                                @ApiParam(value = "0未提交 1已提交", required = false) @RequestParam(required = false) Integer arrangeSubmit) {
        return ResponseObject.ofSuccess(scheduleService.arrangeDetermineStatistics(taskId, collegeId, keyWord, arrangeSubmit));
    }

    @ApiOperation("获取未排节次")
    @GetMapping(value = "/getUnArrangeSchedule")
    public ResponseObject<List<ScheduleUnitGroupVo>> getUnArrangeSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(scheduleService.getUnArrangeSchedule(taskId));
    }


    @ApiOperation("导出课程编排教学班")
    @GetMapping(value = "/excel/course")
    @LogAnnotation(operationType = "14", operationContent = "导出课程编排教学班")
    public ResponseObject<String> courseToExcel(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                @ApiParam(value = "排课状态 1已排（正常）、2已排（冲突）、3待排", required = false) @RequestParam(required = false) Integer state,
                                                @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                @ApiParam(value = "开课单位名字 ", required = false) @RequestParam(required = false) String collegeName,
                                                @ApiParam(value = "修读性质 1主 2辅修", required = false) @RequestParam(required = false) Integer studyNature,
                                                @ApiParam(value = "专业性质，1:公共课，2:专业课", required = false) @RequestParam(required = false) Integer majorProp) {
        return ResponseObject.ofSuccess(scheduleService.courseToExcel(taskId, state, collegeId, collegeName, studyNature, majorProp));
    }

    @ApiOperation("导出环节编排教学班")
    @GetMapping(value = "/excel/segment")
    @LogAnnotation(operationType = "14", operationContent = "导出环节编排教学班")
    public ResponseObject<String> segmentToExcel(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                 @ApiParam(value = "1正常  2冲突", required = false) @RequestParam(required = false) Integer state,
                                                 @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                 @ApiParam(value = "开课单位名字 ", required = false) @RequestParam(required = false) String collegeName,
                                                 @ApiParam(value = "修读性质 1主 2辅修", required = false) @RequestParam(required = false) Integer studyNature) {
        return ResponseObject.ofSuccess(scheduleService.segmentToExcel(taskId, state, collegeId, collegeName, studyNature));
    }

}
